<template>

  <!-- 
  <template v-for="(item, index) in props.menuData">
    <el-menu-item v-if="!item.children || item.children.length == 0" :index="`${props.index}-${item.meta.id}`"
      :key="`${props.index}-${item.meta.id}`">
      <el-icon size="20">
        <component :is="item.meta.icon"></component>
        <span>{{ item.meta.name }}</span>
      </el-icon>
    </el-menu-item>  
  </template>  
-->

  <!-- 没有子菜单 -->
  <template v-for="(item, index) in props.menuData">
    <el-menu-item 
      @click=" handleClick(item , `${props.index}-${item.meta.id}` )"
      v-if="!item.children || item.children.length == 0" 
      :index="`${props.index}-${item.meta.id}`"
      :key="`${props.index}-${item.meta.id}`">
      <el-icon size="20">
        <component :is="item.meta.icon"></component>
      </el-icon>
      <template #title>{{ item.meta.name }}</template>
    </el-menu-item>
    <!-- 有子菜单 -->
    <el-sub-menu v-else :index="`${props.index}-${item.meta.id}`">
      <template #title>
        <el-icon aria-setsize="20">
          <component :is="item.meta.icon"></component>
        </el-icon>
        <span>{{ item.meta.name }}</span>
      </template>
      <tree-menu :index="`${props.index}-${item.meta.id}`" :menuData="item.children" />
    </el-sub-menu>
  </template>

</template>

<script setup>
import { Document, Menu as IconMenu, Location, Setting, } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router' 
import { useStore } from 'vuex'
const props = defineProps(['menuData', 'index'])
// console.log(props, 'props')

// 拿到store实例
const store = useStore()
// 创建router实例
const router = useRouter()

// 点击菜单方法
const handleClick = (item,active) => {
  console.log(item, 'item')
  store.commit('addMenu', item.meta)
  router.push(item.meta.path)
}
  



</script>

<style scoped></style>